#include "ScanControlPresetDialog.h"
#include "ui_ScanControlPresetDialog.h"

ScanControlPresetDialog::ScanControlPresetDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ScanControlPresetDialog),
    isDragging(false)
{
    ui->setupUi(this); // 初始化UI，必须在获取控件前调用
    // 设置无边框窗口
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    setProperty("MenuTitle","SK功能预设(S)");
    setProperty("ConfigName","ScanControlPreset");
    // 启用鼠标跟踪以处理拖动事件
    ui->titleBarWidget->setMouseTracking(true);
    setMouseTracking(true);
    
    // 连接按钮信号和槽
    connect(ui->btnConfirm, &QPushButton::clicked, this, &ScanControlPresetDialog::onBtnConfirmClicked);
    connect(ui->btnCancel, &QPushButton::clicked, this, &ScanControlPresetDialog::onBtnCancelClicked);
    connect(ui->btnClose, &QToolButton::clicked, this, &ScanControlPresetDialog::onBtnCancelClicked);
}


ScanControlPresetDialog::~ScanControlPresetDialog()
{
    delete ui;
}

void ScanControlPresetDialog::setUIData(const QVariantMap &paramsMap)
{
    // 设置相机序列号
    if (paramsMap.contains(PARAM_SERIAL_NUMBER)) {
        ui->leSerialNumber->setText(paramsMap[PARAM_SERIAL_NUMBER].toString());
    }
    
    // 设置档位值
    // 档位对应关系：
    // 快0-7 -> 行0-7，列0
    // 中0-7 -> 行0-7，列1
    // 慢0-7 -> 行0-7，列2
    
    // 快0-7
    for (int i = 0; i < 8; ++i) {
        QString key;
        switch (i) {
        case 0: key = PARAM_GEAR_FAST_0; break;
        case 1: key = PARAM_GEAR_FAST_1; break;
        case 2: key = PARAM_GEAR_FAST_2; break;
        case 3: key = PARAM_GEAR_FAST_3; break;
        case 4: key = PARAM_GEAR_FAST_4; break;
        case 5: key = PARAM_GEAR_FAST_5; break;
        case 6: key = PARAM_GEAR_FAST_6; break;
        case 7: key = PARAM_GEAR_FAST_7; break;
        default: continue;
        }
        
        if (paramsMap.contains(key)) {
            QTableWidgetItem *item = ui->twGearSettings->item(i, 0);
            if (!item) {
                item = new QTableWidgetItem();
                ui->twGearSettings->setItem(i, 0, item);
            }
            item->setText(paramsMap[key].toString());
        }
    }
    
    // 中0-7
    for (int i = 0; i < 8; ++i) {
        QString key;
        switch (i) {
        case 0: key = PARAM_GEAR_MEDIUM_0; break;
        case 1: key = PARAM_GEAR_MEDIUM_1; break;
        case 2: key = PARAM_GEAR_MEDIUM_2; break;
        case 3: key = PARAM_GEAR_MEDIUM_3; break;
        case 4: key = PARAM_GEAR_MEDIUM_4; break;
        case 5: key = PARAM_GEAR_MEDIUM_5; break;
        case 6: key = PARAM_GEAR_MEDIUM_6; break;
        case 7: key = PARAM_GEAR_MEDIUM_7; break;
        default: continue;
        }
        
        if (paramsMap.contains(key)) {
            QTableWidgetItem *item = ui->twGearSettings->item(i, 1);
            if (!item) {
                item = new QTableWidgetItem();
                ui->twGearSettings->setItem(i, 1, item);
            }
            item->setText(paramsMap[key].toString());
        }
    }
    
    // 慢0-7
    for (int i = 0; i < 8; ++i) {
        QString key;
        switch (i) {
        case 0: key = PARAM_GEAR_SLOW_0; break;
        case 1: key = PARAM_GEAR_SLOW_1; break;
        case 2: key = PARAM_GEAR_SLOW_2; break;
        case 3: key = PARAM_GEAR_SLOW_3; break;
        case 4: key = PARAM_GEAR_SLOW_4; break;
        case 5: key = PARAM_GEAR_SLOW_5; break;
        case 6: key = PARAM_GEAR_SLOW_6; break;
        case 7: key = PARAM_GEAR_SLOW_7; break;
        default: continue;
        }
        
        if (paramsMap.contains(key)) {
            QTableWidgetItem *item = ui->twGearSettings->item(i, 2);
            if (!item) {
                item = new QTableWidgetItem();
                ui->twGearSettings->setItem(i, 2, item);
            }
            item->setText(paramsMap[key].toString());
        }
    }
    
    // 设置开关状态
    // 注意：这里假设SwitchSliderWidget有setChecked方法
    if (paramsMap.contains(PARAM_SHUTTER_ENABLE)) {
        ui->swShutterEnable->setChecked(paramsMap[PARAM_SHUTTER_ENABLE].toBool());
    }
    if (paramsMap.contains(PARAM_GAIN1_ENABLE)) {
        ui->swGain1Enable->setChecked(paramsMap[PARAM_GAIN1_ENABLE].toBool());
    }
    if (paramsMap.contains(PARAM_GAIN2_ENABLE)) {
        ui->swGain2Enable->setChecked(paramsMap[PARAM_GAIN2_ENABLE].toBool());
    }
    if (paramsMap.contains(PARAM_MCP_GATE_ENABLE)) {
        ui->swMCPGateEnable->setChecked(paramsMap[PARAM_MCP_GATE_ENABLE].toBool());
    }
    if (paramsMap.contains(PARAM_CATHODE_CONTROL_ENABLE)) {
        ui->swCathodeControlEnable->setChecked(paramsMap[PARAM_CATHODE_CONTROL_ENABLE].toBool());
    }
}

QVariantMap ScanControlPresetDialog::getUIData()
{
    QVariantMap paramsMap;
    
    // 获取相机序列号
    paramsMap[PARAM_SERIAL_NUMBER] = ui->leSerialNumber->text();
    
    // 获取档位值
    // 档位对应关系：
    // 快0-7 -> 行0-7，列0
    // 中0-7 -> 行0-7，列1
    // 慢0-7 -> 行0-7，列2
    
    // 快0-7
    for (int i = 0; i < 8; ++i) {
        QTableWidgetItem *item = ui->twGearSettings->item(i, 0);
        QString value = item ? item->text() : "0";
        
        switch (i) {
        case 0: paramsMap[PARAM_GEAR_FAST_0] = value; break;
        case 1: paramsMap[PARAM_GEAR_FAST_1] = value; break;
        case 2: paramsMap[PARAM_GEAR_FAST_2] = value; break;
        case 3: paramsMap[PARAM_GEAR_FAST_3] = value; break;
        case 4: paramsMap[PARAM_GEAR_FAST_4] = value; break;
        case 5: paramsMap[PARAM_GEAR_FAST_5] = value; break;
        case 6: paramsMap[PARAM_GEAR_FAST_6] = value; break;
        case 7: paramsMap[PARAM_GEAR_FAST_7] = value; break;
        default: break;
        }
    }
    
    // 中0-7
    for (int i = 0; i < 8; ++i) {
        QTableWidgetItem *item = ui->twGearSettings->item(i, 1);
        QString value = item ? item->text() : "0";
        
        switch (i) {
        case 0: paramsMap[PARAM_GEAR_MEDIUM_0] = value; break;
        case 1: paramsMap[PARAM_GEAR_MEDIUM_1] = value; break;
        case 2: paramsMap[PARAM_GEAR_MEDIUM_2] = value; break;
        case 3: paramsMap[PARAM_GEAR_MEDIUM_3] = value; break;
        case 4: paramsMap[PARAM_GEAR_MEDIUM_4] = value; break;
        case 5: paramsMap[PARAM_GEAR_MEDIUM_5] = value; break;
        case 6: paramsMap[PARAM_GEAR_MEDIUM_6] = value; break;
        case 7: paramsMap[PARAM_GEAR_MEDIUM_7] = value; break;
        default: break;
        }
    }
    
    // 慢0-7
    for (int i = 0; i < 8; ++i) {
        QTableWidgetItem *item = ui->twGearSettings->item(i, 2);
        QString value = item ? item->text() : "0";
        
        switch (i) {
        case 0: paramsMap[PARAM_GEAR_SLOW_0] = value; break;
        case 1: paramsMap[PARAM_GEAR_SLOW_1] = value; break;
        case 2: paramsMap[PARAM_GEAR_SLOW_2] = value; break;
        case 3: paramsMap[PARAM_GEAR_SLOW_3] = value; break;
        case 4: paramsMap[PARAM_GEAR_SLOW_4] = value; break;
        case 5: paramsMap[PARAM_GEAR_SLOW_5] = value; break;
        case 6: paramsMap[PARAM_GEAR_SLOW_6] = value; break;
        case 7: paramsMap[PARAM_GEAR_SLOW_7] = value; break;
        default: break;
        }
    }
    
    // 获取开关状态
    // 注意：这里假设SwitchSliderWidget有isChecked方法
    paramsMap[PARAM_SHUTTER_ENABLE] = ui->swShutterEnable->isChecked();
    paramsMap[PARAM_GAIN1_ENABLE] = ui->swGain1Enable->isChecked();
    paramsMap[PARAM_GAIN2_ENABLE] = ui->swGain2Enable->isChecked();
    paramsMap[PARAM_MCP_GATE_ENABLE] = ui->swMCPGateEnable->isChecked();
    paramsMap[PARAM_CATHODE_CONTROL_ENABLE] = ui->swCathodeControlEnable->isChecked();
    
    return paramsMap;
}

void ScanControlPresetDialog::onBtnConfirmClicked()
{
    // 直接关闭对话框，返回确认状态
    accept();
}

void ScanControlPresetDialog::onBtnCancelClicked()
{
    // 直接关闭对话框，返回取消状态
    reject();
}

void ScanControlPresetDialog::mousePressEvent(QMouseEvent *event)
{
    // 只有在标题栏区域点击并且是左键点击时才开始拖动
    if (ui->titleBarWidget->geometry().contains(event->pos()) && 
        event->button() == Qt::LeftButton) {
        isDragging = true;
        dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
    QDialog::mousePressEvent(event);
}

void ScanControlPresetDialog::mouseMoveEvent(QMouseEvent *event)
{
    // 如果正在拖动，则移动窗口
    if (isDragging && (event->buttons() & Qt::LeftButton)) {
        move(event->globalPos() - dragPosition);
        event->accept();
    }
    QDialog::mouseMoveEvent(event);
}

void ScanControlPresetDialog::mouseReleaseEvent(QMouseEvent *event)
{
    // 释放鼠标时停止拖动
    if (isDragging && event->button() == Qt::LeftButton) {
        isDragging = false;
        event->accept();
    }
    QDialog::mouseReleaseEvent(event);
}

